package ui.graphic;

import Modelos.ModeloCartas;
import Modelos.ModeloTabuleiro;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

/**
 *
 * @author gustavodinis
 */
public class InfoLateral extends JPanel implements Observer {

    ModeloTabuleiro mT;
    ModeloCartas mC;
    Image logo;
    URL logoPath;
    String jogadorActivo;
    JLabel descricao;
    JButton nextPlayer;

    public InfoLateral(ModeloTabuleiro mT, ModeloCartas mC) {
        this.mC = mC;
        this.mT = mT;
        mC.addObserver(this);
        mT.addObserver(this);
        BoxLayout layout= new BoxLayout(this, BoxLayout.Y_AXIS);
        setLayout(layout);
        setSize(200, 671);
        setPreferredSize(new Dimension(190, 671));
        
        descricao = new JLabel();
        descricao.setLocation(2, 190);
        descricao.setAlignmentY(CENTER_ALIGNMENT);
        
        add(descricao);
        setBackground(Color.WHITE);
        logoPath = getClass().getResource("resources/images/logo.jpg");
        nextPlayer = new JButton("Fim turno");
        add(Box.createRigidArea(new Dimension(0,240)));
        
            add(nextPlayer);
//        nextPlayer.setLocation(100,600);
        //add(nextPlayer);
        try {
            logo = ImageIO.read(logoPath);
            logo = makeColorTransparent(logo, Color.WHITE);
        } catch (IOException ex) {
            System.out.println("imagem erro");
        }
        motionList();
        repaint();
        validate();

    }

    @Override
    public void update(Observable o, Object arg) {
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(logo, 10, 10, 170, 75, this);
        //g2d.setFont(new Font("Times New Roman", Font.BOLD, 20));
        g2d.setFont(new Font("MontereyMediumFLF", Font.PLAIN, 15));
        g2d.drawString("Informação Lateral", 32, 100);
        g2d.drawString("Turno do Jogador:", 20, 140);
        g2d.drawString(mT.getActivePlayer().getName(), 20, 160);
        g2d.drawString("Descricao Carta: ", 20, 180);
        descricao.setText("");
        
        if (mC.getlast() != null) {
            g2d.drawString(mC.getlast().getDescrition(), 2, 200);
            g2d.drawString("Movimentos Restantes:" + mC.getlast().getNumAction(), 2, 220);
//            g2d.drawString(mC.getlast().getDescrition().substring(0, (mC.getlast().getDescrition().length() / 2)), 0, 180);
//            g2d.drawString(mC.getlast().getDescrition().substring(mC.getlast().getDescrition().length() / 2, (mT.getActivePlayer().getName().length())), 0, 196);
        }
        
        
        //new JLabel(, 2);

    }

    public static Image makeColorTransparent(Image im, final Color color) {
        ImageFilter filter = new RGBImageFilter() {
            // the color we are looking for... Alpha bits are set to opaque
            public int markerRGB = color.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ((rgb | 0xFF000000) == markerRGB) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                } else {
                    // nothing to do
                    return rgb;
                }
            }
        };

        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }

    public void motionList() {

        final MouseAdapter next;

        next = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                mT.proxPlayer();
            }
        };
        nextPlayer.addMouseListener(next);
    }
}
